package com.wangyi.view;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wangyi.activity.R;
import com.wangyi.sdk.Comment;
import com.wangyi.util.Util;

public class CommentListAdapter extends BaseAdapter {
	private final Context context;
	private final List<Comment> commentList;
	public final static int LOADPICFINISH = 1;
	public int height = 0;

	public CommentListAdapter(Context context, List<Comment> commentList) {
		super();
		this.context = context;
		this.commentList = commentList;
	}

	public int getHeight() {
		return height;
	}

	@Override
	public int getCount() {
		return commentList.size();
	}

	@Override
	public Object getItem(int position) {
		return commentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HoldView holdView = null;
		if (commentList.size() > 0) {
			if (convertView == null) {
				holdView = new HoldView();
				convertView = LayoutInflater.from(context).inflate(R.layout.commentadapter, null);
				holdView.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
				holdView.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
				holdView.comment_tv = (TextView) convertView.findViewById(R.id.comment_tv);
				holdView.pic_img = (ImageView) convertView.findViewById(R.id.pic_img);
				convertView.setTag(holdView);
			} else {
				holdView = (HoldView) convertView.getTag();
			}
			holdView.name_tv.setText(commentList.get(position).getUser().getName());
			holdView.time_tv.setText(Util.convertDateToString(commentList.get(position).getCreatedAt()));
			holdView.comment_tv.setText(commentList.get(position).getText());
			String pic_url = commentList.get(position).getUser().getProfile_image_url();
			Util.loadImage(holdView.pic_img, pic_url, convertView);
			height += convertView.getMeasuredHeight();
			Log.i("height","height:"+ height+"_convertView.getMeasuredHeight:"+convertView.getMeasuredHeight());
		}
		return convertView;
	}

	static class HoldView {
		private TextView name_tv;
		private TextView time_tv;
		private TextView comment_tv;
		private ImageView pic_img;
	}

}
